﻿namespace Pub.Class
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Web.UI;

    public class Pager
    {
        private Align _Align;
        private int _Index;
        private int _PageIndex;
        private int _PageSize;
        private string _StyleName;
        private int _TotalPage;
        private int _TotalRecord;
        private string _Url;
        private StringBuilder sbData;

        public Pager()
        {
            this._Index = 1;
            this.sbData = new StringBuilder();
            this._Url = "";
            this._PageIndex = 1;
            this._TotalPage = 1;
            this._PageSize = 20;
            this._TotalRecord = 0;
            this._StyleName = "BasePager";
            this._Align = Align.Left;
        }

        public Pager(string Url, int PageIndex, int TotalPage, int PageSize, int TotalRecord, string StyleName)
        {
            this._Index = 1;
            this.sbData = new StringBuilder();
            this._Url = Url;
            this._PageIndex = PageIndex;
            this._TotalPage = TotalPage;
            this._PageSize = PageSize;
            this._TotalRecord = TotalRecord;
            this._StyleName = StyleName;
        }

        public static string GetBasePager(int curPage, int countPage, string url, int extendPage)
        {
            if (countPage <= 1)
            {
                return "";
            }
            StringBuilder builder = new StringBuilder("");
            if (curPage == 1)
            {
                builder.AppendFormat("<span>{0}</span>&nbsp;<span>{1}</span>&nbsp;<a href=\"{2}\">{3}</a>&nbsp;<a href=\"{4}\">{5}</a>", new object[] { "[首页]", "[上一页]", string.Format(url, curPage + 1), "[下一页]", string.Format(url, countPage), "[尾页]" });
            }
            else if (curPage == countPage)
            {
                builder.AppendFormat("<a href=\"{2}\">{3}</a>&nbsp;<a href=\"{4}\">{5}</a>&nbsp;<span>{0}</span>&nbsp;<span>{1}</span>", new object[] { "[下一页]", "[尾页]", string.Format(url, 1), "[首页]", string.Format(url, curPage - 1), "[上一页]" });
            }
            else
            {
                builder.AppendFormat("<a href=\"{0}\">{1}</a>&nbsp;<a href=\"{2}\">{3}</a>&nbsp;<a href=\"{4}\">{5}</a>&nbsp;<a href=\"{6}\">{7}</a>", new object[] { string.Format(url, 1), "[首页]", string.Format(url, curPage - 1), "[上一页]", string.Format(url, curPage + 1), "[下一页]", string.Format(url, countPage), "[尾页]" });
            }
            return builder.ToString();
        }

        public static string GetBasePager2(int curPage, int countPage, string url, int extendPage)
        {
            if (countPage <= 1)
            {
                return "";
            }
            StringBuilder builder = new StringBuilder("");
            if (curPage == 1)
            {
                builder.AppendFormat("<span>{0}</span>&nbsp;<span>{1}</span>{6}&nbsp;<a href=\"{2}\">{3}</a>&nbsp;<a href=\"{4}\">{5}</a>", new object[] { "首页", "上一页", string.Format(url, curPage + 1), "下一页", string.Format(url, countPage), "尾页", GetPager3(curPage, countPage, url, extendPage) });
            }
            else if (curPage == countPage)
            {
                builder.AppendFormat("<a href=\"{2}\">{3}</a>&nbsp;<a href=\"{4}\">{5}</a>{6}&nbsp;<span>{0}</span>&nbsp;<span>{1}</span>", new object[] { "下一页", "尾页", string.Format(url, 1), "首页", string.Format(url, curPage - 1), "上一页", GetPager3(curPage, countPage, url, extendPage) });
            }
            else
            {
                builder.AppendFormat("<a href=\"{0}\">{1}</a>&nbsp;<a href=\"{2}\">{3}</a>{8}&nbsp;<a href=\"{4}\">{5}</a>&nbsp;<a href=\"{6}\">{7}</a>", new object[] { string.Format(url, 1), "首页", string.Format(url, curPage - 1), "上一页", string.Format(url, curPage + 1), "下一页", string.Format(url, countPage), "尾页", GetPager3(curPage, countPage, url, extendPage) });
            }
            return builder.ToString();
        }

        public string GetData(string CtrlID)
        {
            this._TotalPage = this._TotalRecord / this._PageSize;
            if ((this._TotalRecord % this._PageSize) != 0)
            {
                this._TotalPage++;
            }
            this.sbData.Remove(0, this.sbData.Length);
            this.sbData.Append("var myPager" + this._Index.ToString() + " = new " + this._StyleName + "();\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setUrl(\"" + this._Url + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageIndex(" + this._PageIndex.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalPage(" + this._TotalPage.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageSize(" + this._PageSize.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalRecord(" + this._TotalRecord.ToString() + ");\n");
            string str = "";
            switch (this._Align)
            {
                case Align.Left:
                    str = "left";
                    break;

                case Align.Center:
                    str = "center";
                    break;

                case Align.Right:
                    str = "right";
                    break;
            }
            this.sbData.Append("myPager" + this._Index.ToString() + ".setAlign(\"" + str + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".Execute(\"" + CtrlID + "\");\n");
            return this.sbData.ToString();
        }

        public static string GetDiscuzNTPager(int curPage, int countPage, string url, out string jsCode, int extendPage = 8)
        {
            StringBuilder builder = new StringBuilder();
            if (countPage != 1)
            {
                string str = GetPager(curPage, countPage, url, extendPage);
                if (str.IndexOf("&laquo;") > 0)
                {
                    builder.AppendFormat("<a class=\"prev\" href=\"{0}\">上一页</a>", string.Format(url, curPage - 1));
                }
                builder.Append(str);
                builder.AppendFormat("<kbd><input name=\"gopage\" type=\"text\" class=\"txt\" title=\"可以输入页码按回车键自动跳转\" value=\"{0}\" onfocus=\"this.value=this.defaultValue;this.select();\" onKeyDown=\"pageinputOnKeyDown(this,event);\" size=\"2\" maxlength=\"9\" />/ {1}</kbd>", curPage, countPage);
                if (curPage != countPage)
                {
                    builder.AppendFormat("<a class=\"next\" href=\"{0}\">下一页</a>", string.Format(url, curPage + 1));
                }
            }
            jsCode = "function pageinputOnKeyDown(obj, event) { if (event.keyCode == 13) { window.location = '{0}'.format(parseInt(obj.value) > 0 ? parseInt(obj.value) : 1); }; return (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 97 && event.keyCode <= 105) || event.keyCode == 8; };";
            return builder.ToString();
        }

        public static string GetDiscuzNTPager(int curPage, int countPage, string url, out string jsCode, int extendPage, int totals)
        {
            StringBuilder builder = new StringBuilder();
            jsCode = "";
            if (countPage <= 1)
            {
                return "";
            }
            if (countPage != 1)
            {
                string str = GetPager(curPage, countPage, url, extendPage);
                if (str.IndexOf("&laquo;") > 0)
                {
                    builder.AppendFormat("<a class=\"prev\" href=\"{0}\">上一页</a>", string.Format(url, curPage - 1));
                }
                builder.Append(str);
                builder.AppendFormat("<input name=\"gopage\" type=\"text\" class=\"txt\" title=\"可以输入页码按回车键自动跳转\" onfocus=\"this.value=this.defaultValue;this.select();\" onKeyDown=\"pageinputOnKeyDown(this,event);\" size=\"2\" maxlength=\"9\" /> /&nbsp;{1}页&nbsp; {2}条记录&nbsp; ", curPage, countPage, totals);
                if (curPage != countPage)
                {
                    builder.AppendFormat("<a class=\"next\" href=\"{0}\">下一页</a>", string.Format(url, curPage + 1));
                }
            }
            jsCode = "function pageinputOnKeyDown(obj, event) { if (event.keyCode == 13) { window.location = '" + url + "'.format(parseInt(obj.value) > 0 ? parseInt(obj.value) : 1); }; return (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 97 && event.keyCode <= 105) || event.keyCode == 8; };";
            return builder.ToString();
        }

        public static string GetImgPager(int curPage, int countPage, string url, int extendPage)
        {
            if (countPage <= 1)
            {
                return "";
            }
            int num = 1;
            int num2 = 1;
            string str = "<a class=\"first firstenable\" href=\"" + string.Format(url, 1) + "\"></a>";
            string str2 = "<a class=\"last lastenable\" href=\"" + string.Format(url, countPage) + "\"></a>";
            if (countPage < 1)
            {
                countPage = 1;
            }
            if (extendPage < 3)
            {
                extendPage = 2;
            }
            if (countPage > extendPage)
            {
                if ((curPage - (extendPage / 2)) > 0)
                {
                    if ((curPage + (extendPage / 2)) < countPage)
                    {
                        num = curPage - (extendPage / 2);
                        num2 = (num + extendPage) - 1;
                    }
                    else
                    {
                        num2 = countPage;
                        num = (num2 - extendPage) + 1;
                        str2 = "";
                    }
                }
                else
                {
                    num2 = extendPage;
                }
            }
            else
            {
                num = 1;
                num2 = countPage;
                if (curPage <= 1)
                {
                    str = "<a class=\"first\"></a>";
                }
                if (curPage >= countPage)
                {
                    str2 = "<a class=\"last\"></a>";
                }
            }
            StringBuilder builder = new StringBuilder("");
            builder.Append(str);
            for (int i = num; i <= num2; i++)
            {
                if (i == curPage)
                {
                    builder.Append("<a class=\"active\"></a>");
                }
                else
                {
                    builder.Append("<a href=\"");
                    builder.Append(string.Format(url, i));
                    builder.Append("\"></a>");
                }
            }
            builder.Append(str2);
            return builder.ToString();
        }

        public static string GetPager(int curPage, int countPage, string url, int extendPage)
        {
            if (countPage <= 1)
            {
                return "";
            }
            int num = 1;
            int num2 = 1;
            string str = "<a href=\"" + string.Format(url, 1) + "\">&laquo;</a>&nbsp;";
            string str2 = "<a href=\"" + string.Format(url, countPage) + "\">&raquo;</a>&nbsp;";
            if (countPage < 1)
            {
                countPage = 1;
            }
            if (extendPage < 3)
            {
                extendPage = 2;
            }
            if (countPage > extendPage)
            {
                if ((curPage - (extendPage / 2)) > 0)
                {
                    if ((curPage + (extendPage / 2)) < countPage)
                    {
                        num = curPage - (extendPage / 2);
                        num2 = (num + extendPage) - 1;
                    }
                    else
                    {
                        num2 = countPage;
                        num = (num2 - extendPage) + 1;
                        str2 = "";
                    }
                }
                else
                {
                    num2 = extendPage;
                    str = "";
                }
            }
            else
            {
                num = 1;
                num2 = countPage;
                str = "";
                str2 = "";
            }
            StringBuilder builder = new StringBuilder("");
            builder.Append(str);
            for (int i = num; i <= num2; i++)
            {
                if (i == curPage)
                {
                    builder.Append("&nbsp;<span>");
                    builder.Append(i);
                    builder.Append("</span>&nbsp;");
                }
                else
                {
                    builder.Append("&nbsp;<a href=\"");
                    builder.Append(string.Format(url, i));
                    builder.Append("\">");
                    builder.Append(i);
                    builder.Append("</a>&nbsp;");
                }
            }
            builder.Append(str2);
            return builder.ToString();
        }

        public static string GetPager2(int curPage, int countPage, string url, int extendPage = 8)
        {
            StringBuilder builder = new StringBuilder();
            if (countPage != 1)
            {
                string str = GetPager(curPage, countPage, url, extendPage);
                if (str.IndexOf("&laquo;") > 0)
                {
                    builder.AppendFormat("<a class=\"prev\" href=\"{0}\">上一页</a>", string.Format(url, curPage - 1));
                }
                builder.Append(str);
                if (curPage != countPage)
                {
                    builder.AppendFormat("<a class=\"next\" href=\"{0}\">下一页</a>", string.Format(url, curPage + 1));
                }
            }
            return builder.ToString();
        }

        public static string GetPager3(int curPage, int countPage, string url, int extendPage)
        {
            if (countPage <= 1)
            {
                return "";
            }
            int num = 1;
            int num2 = 1;
            if (countPage < 1)
            {
                countPage = 1;
            }
            if (extendPage < 3)
            {
                extendPage = 2;
            }
            if (countPage > extendPage)
            {
                if ((curPage - (extendPage / 2)) > 0)
                {
                    if ((curPage + (extendPage / 2)) < countPage)
                    {
                        num = curPage - (extendPage / 2);
                        num2 = (num + extendPage) - 1;
                    }
                    else
                    {
                        num2 = countPage;
                        num = (num2 - extendPage) + 1;
                    }
                }
                else
                {
                    num2 = extendPage;
                }
            }
            else
            {
                num = 1;
                num2 = countPage;
            }
            StringBuilder builder = new StringBuilder("");
            for (int i = num; i <= num2; i++)
            {
                if (i == curPage)
                {
                    builder.Append("&nbsp;<span class=\"active\">");
                    builder.Append(i);
                    builder.Append("</span>");
                }
                else
                {
                    builder.Append("&nbsp;<a href=\"");
                    builder.Append(string.Format(url, i));
                    builder.Append("\">");
                    builder.Append(i);
                    builder.Append("</a>");
                }
            }
            return builder.ToString();
        }

        public void SetData(Page Page, string CtrlID)
        {
            this._TotalPage = this._TotalRecord / this._PageSize;
            if ((this._TotalRecord % this._PageSize) != 0)
            {
                this._TotalPage++;
            }
            this.sbData.Remove(0, this.sbData.Length);
            this.sbData.Append("var myPager" + this._Index.ToString() + " = new " + this._StyleName + "();\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setUrl(\"" + this._Url + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageIndex(" + this._PageIndex.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalPage(" + this._TotalPage.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setPageSize(" + this._PageSize.ToString() + ");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".setTotalRecord(" + this._TotalRecord.ToString() + ");\n");
            string str = "";
            switch (this._Align)
            {
                case Align.Left:
                    str = "left";
                    break;

                case Align.Center:
                    str = "center";
                    break;

                case Align.Right:
                    str = "right";
                    break;
            }
            this.sbData.Append("myPager" + this._Index.ToString() + ".setAlign(\"" + str + "\");\n");
            this.sbData.Append("myPager" + this._Index.ToString() + ".Execute(\"" + CtrlID + "\");\n");
            Js.Run(Page, this.sbData.ToString(), false, "Js" + this._Index.ToString());
        }

        public Align HAlign
        {
            set
            {
                this._Align = value;
            }
        }

        public int Index
        {
            set
            {
                this._Index = value;
            }
        }

        public int PageIndex
        {
            set
            {
                this._PageIndex = value;
            }
        }

        public int PageSize
        {
            set
            {
                this._PageSize = value;
            }
        }

        public string StyleName
        {
            set
            {
                this._StyleName = value;
            }
        }

        public int TotalPage
        {
            set
            {
                this._TotalPage = value;
            }
        }

        public int TotalRecord
        {
            set
            {
                this._TotalRecord = value;
            }
        }

        public string Url
        {
            set
            {
                this._Url = value;
            }
        }

        public enum Align
        {
            Left,
            Center,
            Right
        }
    }
}

